<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        @click="$router.push('/search')"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
    <!-- 
      animated 滑动的动画
      border 底边框线
      swipeable 开启左右手势滑动
     -->
    <van-tabs  class="channel-tabs" v-model="active" swipeable animated border>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <!-- {{item.name}}的列表数据 -->
        <!-- 文章列表组件 -->
        <article-list :channel="item" />
      </van-tab>
      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 右侧按钮 -->
      <div slot="nav-right" @click="isChannelEidtShow=true" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->

    <!-- 频道编辑弹框 -->
    <van-popup 
      v-model="isChannelEidtShow" 
      closeable 
      close-icon="close" 
      close-icon-position="top-left" 
      position="bottom" 
      :style="{ height: '100%' }" 
    >
      <!-- 
        :active-index="active"   父子通信
        @update:active-index="active=$event"  子父通信
        
        :props属性名="父的变量"
        @update:props属性名="父的变量=$event"
        
        this.$emit('update:props属性名',实参)

        简写为：
        :active-index.sync="active"
        :props属性名.sync="父的变量"
      -->
      <channel-edit 
        :user-channels="channels" 
        :active-index.sync="active"
        @close-popup="isChannelEidtShow=false"
      />
    </van-popup>
    <!-- /频道编辑弹框 -->

  </div>
</template>

<script>
import { getUserChannels }  from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      active:0, // 激活的下标位置
      channels:[],  // 我的频道信息
      isChannelEidtShow:false // 频道修改弹框是否显示
    }
  },
  mounted () {
    // 加载数据
    this.loadChannels()
  },
  methods: {
    // 获取频道列表
    async loadChannels(){
      try {
        // 是否登录
        if(!this.user){
          // 没有登录，判断本地存储是否有频道数据
          if(getItem('TOUTIAO_CHANNELS')){
            //  本地存储有就用本地存储的
            this.channels = getItem('TOUTIAO_CHANNELS')
          }else{
            // 使用线上的默认频道数据
            let { data:res } = await getUserChannels()
            this.channels = res.data.channels
          }
        }else{
          // 登录了，拉取线上数据
          let { data:res } = await getUserChannels()
          this.channels = res.data.channels
        }
      } catch (error) {
        // 错误处理
        this.$toast.fail('获取频道失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  // 深度选择器问题
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  /deep/ .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
  }
  /deep/ .van-tabs__line {
    width: 32px;
    height: 6px;
    background-color: #3296fa;
    bottom: 40px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/img/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
